package cn.yinyu.queue.module.ecg.controller.admin.checktype;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.yinyu.queue.framework.common.pojo.PageParam;
import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.framework.common.pojo.CommonResult;
import cn.yinyu.queue.framework.common.util.object.BeanUtils;
import static cn.yinyu.queue.framework.common.pojo.CommonResult.success;

import cn.yinyu.queue.framework.excel.core.util.ExcelUtils;

import cn.yinyu.queue.framework.apilog.core.annotation.ApiAccessLog;
import static cn.yinyu.queue.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.yinyu.queue.module.ecg.controller.admin.checktype.vo.*;
import cn.yinyu.queue.module.ecg.dal.dataobject.checktype.CheckTypeDO;
import cn.yinyu.queue.module.ecg.service.checktype.CheckTypeService;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 检查类型")
@RestController
@RequestMapping("/ecg/check-type")
@Validated
public class CheckTypeController {

    @Resource
    private CheckTypeService checkTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建检查类型")
    @PreAuthorize("@ss.hasPermission('ecg:check-type:create')")
    public CommonResult<Integer> createCheckType(@Valid @RequestBody CheckTypeSaveReqVO createReqVO) {
        return success(checkTypeService.createCheckType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新检查类型")
    @PreAuthorize("@ss.hasPermission('ecg:check-type:update')")
    public CommonResult<Boolean> updateCheckType(@Valid @RequestBody CheckTypeSaveReqVO updateReqVO) {
        checkTypeService.updateCheckType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除检查类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ecg:check-type:delete')")
    public CommonResult<Boolean> deleteCheckType(@RequestParam("id") Integer id) {
        checkTypeService.deleteCheckType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得检查类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ecg:check-type:query')")
    public CommonResult<CheckTypeRespVO> getCheckType(@RequestParam("id") Integer id) {
        CheckTypeDO checkType = checkTypeService.getCheckType(id);
        return success(BeanUtils.toBean(checkType, CheckTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得检查类型分页")
    @PreAuthorize("@ss.hasPermission('ecg:check-type:query')")
    public CommonResult<PageResult<CheckTypeRespVO>> getCheckTypePage(@Valid CheckTypePageReqVO pageReqVO) {
        PageResult<CheckTypeDO> pageResult = checkTypeService.getCheckTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CheckTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出检查类型 Excel")
    @PreAuthorize("@ss.hasPermission('ecg:check-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCheckTypeExcel(@Valid CheckTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CheckTypeDO> list = checkTypeService.getCheckTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "检查类型.xls", "数据", CheckTypeRespVO.class,
                        BeanUtils.toBean(list, CheckTypeRespVO.class));
    }

    @GetMapping("/list-simple-check-type")
    @Operation(summary = "获得简单检查类型列表")
    @PermitAll
    public CommonResult<List<SimpleCheckTypeRespVO>> getSimpleCheckTypeList() {
        List<CheckTypeDO> list = checkTypeService.getSimpleCheckTypeList();
        return success(BeanUtils.toBean(list, SimpleCheckTypeRespVO.class));
    }
}
